package com.example.showlocation;

import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class RequestPermissionActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request_permission);
    }
    @SuppressLint("IntentReset")
    public void RequestPermission(View view) throws Exception {
        String[] strings=new String[1];
        strings[0]="android.permission.ACCESS_FINE_LOCATION";
        if (ContextCompat.checkSelfPermission(
                getApplicationContext(), "android.permission.ACCESS_FINE_LOCATION") ==
                PackageManager.PERMISSION_GRANTED) {

        } else if (ActivityCompat.shouldShowRequestPermissionRationale(
                this, "android.permission.ACCESS_FINE_LOCATION")) {

        } else {

            ActivityCompat.requestPermissions(this, strings , 0);
            Log.d(getLocalClassName()+":", "RequestPermission.else");
        }
        Log.d(getLocalClassName()+":", "RequestPermission");
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case 0:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 &&
                        grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(getLocalClassName()+":", "onRequestPermissionsResult.if");
                    // Permission is granted. Continue the action or workflow
                    // in your app.
                } else {
                    Log.d(getLocalClassName()+":", "onRequestPermissionsResult.else");
                    // Explain to the user that the feature is unavailable because
                    // the feature requires a permission that the user has denied.
                    // At the same time, respect the user's decision. Don't link to
                    // system settings in an effort to convince the user to change
                    // their decision.
                }
                return;
        }
        // Other 'case' lines to check for other
        // permissions this app might request.
    }
}
 /*strings[2]="android.permission.ADD_VOICEMAIL";
        strings[3]="android.permission.BLUETOOTH";
        strings[0]="android.permission.ACCESS_BACKGROUND_LOCATION";
        strings[1]="android.permission.ACCESS_COARSE_LOCATION";
        strings[2]="android.permission.ACCESS_FINE_LOCATION";*/